home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / search / lsmtool-.6 / lsmtool- / lsmtool-0.6 / lsmsort.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  2KB  |  69 lines

  1. /*
  2.  * lsmsort.c -- sort an LSM database by the title field
  3.  *
  4.  * Lars Wirzenius
  5.  * "@(#)lsmtool:lsmsort.c,v 1.2 1994/11/20 18:18:22 wirzeniu Exp"
  6.  */
  7.  
  8.  
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <curses.h>
  14. #include <publib.h>
  15. #include "lsm.h"
  16.  
  17.  
  18. /* The database and the current location in it */
  19. static struct lsm_database db;
  20.  
  21.  
  22. static void sort_database(void);
  23.  
  24. int main(int argc, char **argv) {
  25.     __set_liberror(__exit_on_error | __complain_on_error);
  26.     set_progname(argv[0], "lsmsort");
  27.  
  28.     if (lsm_read_database(stdin, &db) == -1)
  29.         errormsg(1, 0, "error reading database");
  30.  
  31.     sort_database();
  32.  
  33.     if (lsm_write_database(stdout, &db) == -1)
  34.         errormsg(1, 0, "error writing in database");
  35.  
  36.     return 0;
  37. }
  38.  
  39.  
  40. /* Sort the database.  Try to keep track of the current entry.  */
  41. static int field = -1;
  42. static int entry_cmp(const void *e1, const void *e2) {
  43.     const struct lsm_entry *ee1 = e1;
  44.     const struct lsm_entry *ee2 = e2;
  45.     assert(field >= 0);
  46.     return strcmp(ee1->fields[field], ee2->fields[field]);
  47. }
  48. static void sort_database(void) {
  49.     int f, i;
  50.     struct lsm_entry *e;
  51.  
  52.     f = lsm_field_to_index("Title");
  53.     field = lsm_field_to_index(" temp ");
  54.     for (i = 0; i < db.nentries; ++i) {
  55.         e = &db.entries[i];
  56.         if (e->fields[f] == NULL)
  57.             e->fields[field] = xstrdup("");
  58.         else
  59.             e->fields[field] = strtrim(xstrdup(e->fields[f]));
  60.     }
  61.  
  62.     qsort(db.entries, db.nentries, sizeof(*db.entries), entry_cmp);
  63.  
  64.     for (i = 0; i < db.nentries; ++i) {
  65.         free(db.entries[i].fields[field]);
  66.         db.entries[i].fields[field] = NULL;
  67.     }
  68. }
  69.